<?php
$sql = 'SELECT * FROM syskeys ORDER BY syskey_name';
$keys = db_loadList( $sql );

$titleBlock = new CTitleBlock('System Lookup Keys', 'preferences.png');
$titleBlock->addButton('Main page', '?m=system' );
$titleBlock->show();

if ($canEdit) {
?>
<script language="javascript">
function doDeleteKey(id) {
	if (confirm( 'Are you sure you want to delete this?' )) {
		f = document.sysKeyFrm;
		f.del.value = 1;
		f.syskey_id.value = id;
		f.submit();
	}
}

function doEditKey(id)
{
	var r = $('idKey_'+id);
	$('idSyskeyID').value = id;
	$('idSyskeyName').value = unescape(r.cells[1].getAttribute('data'));
	$('idSyskeyLabel').value = unescape(r.cells[2].getAttribute('data'));
	Dialog('idEditKey').show();
}
</script>
<div id="idEditKey" class="popup" style="visibility: hidden" autohide="click">
<form name="sysKeyFrm" method="post" action="?m=system&u=syskeys&a=do_syskey_aed">
<input type="hidden" name="del" value="0" />
<input id="idSyskeyID" type="hidden" name="syskey_id" value="0"/>
<table border="0" cellpadding="8" cellspacing="0">
<tr>
	<td bgcolor="#E0E0E0" colspan="2"><b>System key information</b></td>
</tr>
<tr>
	<td>Name</td>
	<td><input id="idSyskeyName" type="text" name="syskey_name" value="" class="text" style="width: 300px"/></td>
</tr>
<tr>
	<td valign="top">Value</td>
	<td><textarea id="idSyskeyLabel" name="syskey_label" rows="5" style="width: 300px"></textarea></td>
</tr>
<tr>
	<td colspan="2" align="center">
		<input type="submit" value="<?=$AppUI->_('Save')?>" class="button"/>
		<input type="button" value="<?=$AppUI->_('Cancel')?>" class="button" onclick="Dialog('idEditKey').hide()"/>
	</td>
</tr>
</table>
</form>
</div>
<? } ?>

<table id="idSyskeyList" border="0" cellpadding="2" cellspacing="0" width="100%" class="list">
<tr>
	<th width="12px">&nbsp;</th>
	<th><?=$AppUI->_('Name')?></th>
	<th><?=$AppUI->_('Label')?></th>
	<th width="12px">&nbsp;</th>
</tr>
<?php

function showRow($id=0, $name='', $label='') {
	GLOBAL $canEdit, $AppUI;
	$s = '<tr id="idKey_'.$id.'" class="second">';
	if ($id == 0 && $canEdit) {
		$s .= '<td valign="top">'.
			'<a href="javascript:doEditKey(0)"><img src="images/common_add.gif" title="'.$AppUI->_('Add').'"></a></td>'.
			'<td colspan="2" data=""></td><td data=""></td>';
	} else {
		$s .= '<td valign="top">';
		if ($canEdit) {
			$s .= '<a href="javascript:doEditKey('.$id.')"><img src="images/edit.gif" title="'.$AppUI->_('Edit').'"></a>';
			$s .= '</td>';
		}
		$s .= '<td data="'.rawurlencode(html_entity_decode($name)).'" valign="top">'.str_replace("\n", '<br/>', $name).'</td>';
		$s .= '<td data="'.rawurlencode(html_entity_decode($label)).'">'.str_replace("\n", '<br/>', $label).'</td>';
		$s .= '<td valign="top">';
		if ($canEdit) {
			$s .= '<a href="javascript:doDeleteKey('.$id.')"><img src="images/delete.gif" title="'.$AppUI->_('Delete').'"></a>';
		}
		$s .= '</td>';
	}
	$s .= '</tr>';
	return $s;
}

foreach ($keys as $row) {
	echo showRow( $row['syskey_id'], $row['syskey_name'], $row['syskey_label'] );
}
echo showRow();
?>
</table>
